package com.zenika.training.wicket.zencontact.pages;

import org.apache.wicket.extensions.markup.html.basic.SmartLinkLabel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;

import com.zenika.training.wicket.zencontact.ZenContactApplication;
import com.zenika.training.wicket.zencontact.model.Contact;

public class ViewContactPage extends ZenContactBasePage {

	public ViewContactPage(final Long contactId) {
		setModel(new CompoundPropertyModel(new LoadableDetachableModel() {
			public Object load() {
				return ZenContactApplication.getContactDao().get(contactId);
			}
		}));

		add(new Label("firstName"));
		add(new Label("lastName"));
		add(new SmartLinkLabel("email"));
		add(new Label("notes"));
		add(new Label("active"));
		add(new Label("birthDate"));
		add(new Label("category"));
		add(new Label("language"));

		add(new Link("edit", getModel()) {
			public void onClick() {
				Contact c = (Contact) getModelObject();
				setResponsePage(new EditContactPage(c.getId()));
			}
		});
		add(new Link("delete", getModel()) {
			public void onClick() {
				Contact c = (Contact) getModelObject();
				ZenContactApplication.getContactDao().delete(c.getId());
				setResponsePage(ListContactsPage.class);
			}
		});
	}

	protected String getPageTitle() {
		return "View contact";
	}

}
